﻿Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms

Public Class DataGridView
    Inherits System.Windows.Forms.DataGridView
    Private m_RowsDefaultBackColor As Color = Color.FromArgb(255, 255, 255)
    <Description("行单元格背景色")> _
    <Category("Extend Property")> _
    Public Property RowsDefaultBackColor() As Color
        Get
            Return m_RowsDefaultBackColor
        End Get
        Set(value As Color)
            m_RowsDefaultBackColor = value
        End Set
    End Property

    Private m_AlternatingRowsDefaultBackColor As Color = Color.FromArgb(169, 227, 255)
    <Description("间隔行单元格背景色")> _
    <Category("Extend Property")> _
    Public Property AlternatingRowsDefaultBackColor() As Color
        Get
            Return m_AlternatingRowsDefaultBackColor
        End Get
        Set(value As Color)
            Me.m_AlternatingRowsDefaultBackColor = value
        End Set
    End Property

    Public Sub New()
        MyBase.New()

        Me.ColumnHeadersDefaultCellStyle = New DataGridViewCellStyle() With { _
          .Alignment = DataGridViewContentAlignment.MiddleCenter, _
          .Font = New System.Drawing.Font("宋体", 9.0F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CByte(134)) _
        }
    End Sub

    Protected Overrides Sub OnRowPostPaint(e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs)
        MyBase.OnRowPostPaint(e)

        ' 这是加入记录号的代码
        Using b As New SolidBrush(Me.RowHeadersDefaultCellStyle.ForeColor)
            e.Graphics.DrawString((Convert.ToInt32(e.RowIndex) + 1).ToString(System.Globalization.CultureInfo.CurrentCulture), _
                                  e.InheritedRowStyle.Font, _
                                  b, _
                                  e.RowBounds.Location.X + 20, _
                                  e.RowBounds.Location.Y + 4)
        End Using

        '这是了隔行换色的代码
        Me.RowsDefaultCellStyle.BackColor = Me.RowsDefaultBackColor
        Me.AlternatingRowsDefaultCellStyle.BackColor = Me.AlternatingRowsDefaultBackColor
    End Sub
End Class